﻿// -----------------------------------------------------------------------
//  <copyright file="StoreService.Customer.cs" company="柳柳软件">
//      Copyright (c) 2014-2015 66SOFT. All rights reserved.
//  </copyright>
//  <site>http://www.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2015-12-07 17:51</last-date>
// -----------------------------------------------------------------------

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

using OSharp.Utility;
using OSharp.Utility.Data;

using ZH.JY.Store.Dtos;
using ZH.JY.Store.Models;


namespace ZH.JY.Store
{
    partial class StoreService
    {
        #region Implementation of IStoreContract

        /// <summary>
        /// 获取 客户信息查询数据集
        /// </summary>
        public IQueryable<Customer> Customers
        {
            get { return CustomerRepository.Entities; }
        }

        /// <summary>
        /// 检查客户信息信息是否存在
        /// </summary>
        /// <param name="predicate">检查谓语表达式</param>
        /// <param name="id">更新的客户信息编号</param>
        /// <returns>客户信息是否存在</returns>
        public Task<bool> CheckCustomerExists(Expression<Func<Customer, bool>> predicate, int id = 0)
        {
            predicate.CheckNotNull("predicate");
            return CustomerRepository.CheckExistsAsync(predicate, id);
        }

        /// <summary>
        /// 添加客户信息信息
        /// </summary>
        /// <param name="dtos">要添加的客户信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public Task<OperationResult> CreateCustomers(params CustomerInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return CustomerRepository.InsertAsync(dtos,
                async dto =>
                {
                    if (await CustomerRepository.CheckExistsAsync(m => m.Name == dto.Name))
                    {
                        throw new Exception($"名称为“{dto.Name}”的客户信息已存在。");
                    }
                },
                async (dto, entity) =>
                {
                    if (dto.CustomerType == CustomerType.Chezhu)
                    {
                        if (dto.JiyouId.HasValue && dto.JiyouId.Value > 0)
                        {
                            Product jiyou = await ProductRepository.GetByKeyAsync(dto.JiyouId.Value);
                            entity.Jiyou = jiyou;
                            entity.NextMilage = dto.NowMilage + jiyou.Milage;
                        }
                        else
                        {
                            entity.Jiyou = null;
                        }
                        entity.JiyouGe = dto.JiyouGeId.HasValue && dto.JiyouGeId.Value > 0
                            ? await ProductRepository.GetByKeyAsync(dto.JiyouGeId.Value)
                            : null; 
                    }
                    else
                    {
                        entity.Jiyou = null;
                        entity.JiyouGe = null;
                    }
                    return entity;
                });
        }

        /// <summary>
        /// 更新客户信息信息
        /// </summary>
        /// <param name="dtos">包含更新信息的客户信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public Task<OperationResult> UpdateCustomers(params CustomerInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return CustomerRepository.UpdateAsync(dtos,
                async (dto, entity) =>
                {
                    if (await CustomerRepository.CheckExistsAsync(m => m.Name == dto.Name, dto.Id))
                    {
                        throw new Exception($"名称为“{dto.Name}”的客户信息已存在。");
                    }
                },
                async (dto, entity) =>
                {
                    if (dto.CustomerType == CustomerType.Chezhu)
                    {
                        if (dto.JiyouId.HasValue && dto.JiyouId.Value > 0)
                        {
                            Product product = await ProductRepository.GetByKeyAsync(dto.JiyouId.Value);
                            entity.Jiyou = product;
                            entity.NextMilage = dto.NowMilage + product.Milage;
                        }
                        else
                        {
                            entity.Jiyou = null;
                        }
                        entity.JiyouGe = dto.JiyouGeId.HasValue && dto.JiyouGeId.Value > 0
                            ? await ProductRepository.GetByKeyAsync(dto.JiyouGeId.Value)
                            : null; 
                    }
                    else
                    {
                        entity.Jiyou = null;
                        entity.JiyouGe = null;
                    }
                    return entity;
                });
        }

        /// <summary>
        /// 删除客户信息信息
        /// </summary>
        /// <param name="ids">要删除的客户信息编号</param>
        /// <returns>业务操作结果</returns>
        public Task<OperationResult> DeleteCustomers(params int[] ids)
        {
            ids.CheckNotNull("ids");
            return CustomerRepository.DeleteAsync(ids);
        }

        #endregion
    }
}